/**
 * <pre>
 * Title: 		PropertiesConfigProvider.java
 * Project: 	HP-Common
 * Type:		com.hengpeng.common.configuration.PropertiesConfigProvider
 * Author:		linriqing
 * Create:	 	2007-1-17 13:36:02
 * Copyright: 	Copyright (c) 2007
 * Company:
 * <pre>
 */
package cn.bisoft.java.util.config;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class PropertiesConfigProvider implements ConfigProvider
{
	private static final Log logger = LogFactory.getLog(PropertiesConfigProvider.class);

	private Map configMap = new Properties();

	private final String filepath;

	public PropertiesConfigProvider(String filepath)
	{
		super();
		this.filepath = filepath;
		this.loadAllConfigs();
	}

	public Map getAllConfig()
	{
		return this.configMap;
	}

	public Object getConfigByPrimaryKey(String keyValue)
	{
		return this.configMap.get(keyValue);
	}

	public void reloadAllConfig()
	{
		this.loadAllConfigs();
	}

	public void reloadConfigByPrimaryKey(String keyValue)
	{
		this.reloadAllConfig();
	}

	private void loadAllConfigs()
	{
		this.configMap.clear();
		try
		{
			((Properties) this.configMap).load(Thread.currentThread().getContextClassLoader().getResourceAsStream(
					this.filepath));
		}
		catch (IOException e)
		{
			logger.error(e);
		}
	}

}
